package com.gla.market.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.gla.market.R;
import com.gla.market.entity.ShopItem;
import com.gla.market.entity.Video;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.List;

public class VideoContentAdapter extends BaseAdapter {

    private Context mContext;
    //传过来的数据
    private List<Video> videoItemList;

    public VideoContentAdapter(Context mContext, List<Video> videoItemList){
        this.videoItemList = videoItemList;
        this.mContext = mContext;

    }

    @Override
    public int getCount() {
        return videoItemList != null ? videoItemList.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.video_content_item, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        //数据初始化
        final Video videoItem = videoItemList.get(position);
        if(!"".equals(videoItem.getCover_url())){
            ImageLoaderConfiguration configuration = ImageLoaderConfiguration.createDefault(mContext);
            ImageLoader.getInstance().init(configuration);
            ImageLoader.getInstance().displayImage(videoItem.getCover_url(), holder.videoContentPhoto);
        }
        String videoName = videoItem.getName();
        if(videoName.length() > 6) videoName =  videoName.substring(0,6) + "...";
        holder.videoItemName.setText(videoName);

        String CommodityName = videoItem.getCommodity_name();
        if(CommodityName.length() > 6) CommodityName =  CommodityName.substring(0,6) + "...";

        holder.videoItemGoodsName.setText(CommodityName);

        return convertView;
    }
    class ViewHolder {
        ImageView videoContentPhoto;
        TextView videoItemName,videoItemGoodsName;

        public ViewHolder(View itemView) {
            videoContentPhoto = itemView.findViewById(R.id.video_item_photo);
            videoItemName = itemView.findViewById(R.id.video_item_name);
            videoItemGoodsName = itemView.findViewById(R.id.video_item_goods_name);

        }
    }
}
